﻿using System.Collections.Generic;
using System.Web;
using TA.Turbo.Controls;
using System;
namespace TA.Turbo
{
	public partial class TPage
	{
		#region STATIC
		#region METHODS
		/// <summary>
		/// Ensures that the specified script is properly obfuscated (if needed) and returns the result.
		/// </summary>
		/// <param name="script"></param>
		/// <returns></returns>
		public static string PrepareScript(string script)
		{
			if (script == null) throw new ArgumentNullException("script");

			if (TPage.Current == null) return script;
			return TPage.PrepareScript(script, TPage.Current.OptimizeScripts);
		}
		/// <summary>
		/// Ensures that the specified script is properly obfuscated (if needed) and returns the result.
		/// </summary>
		/// <param name="script"></param>
		/// <param name="optimize"></param>
		/// <returns></returns>
		public static string PrepareScript(string script, bool optimize)
		{
			if (script == null) throw new ArgumentNullException("script");

			if (TPage.Current == null) return script;
			return TPage.Current._obfuscator.Obfuscate(script, optimize);
		}
		#endregion
		#endregion
		#region PROPS
		#region LISTS
		/// <summary>
		/// Add scenarios at run-time. Do so for every request (get or post).
		/// </summary>
		public ScenarioCollection Scenarios
		{
			get
			{
				return _scenarios;
			}
		}
		/// <summary>
		/// Add include files at run-time. Has to be done during Init (not for every request, like during Load).
		/// </summary>
		public IncludeCollection Includes
		{
			get
			{
				return this._includes;
			}
		}
		/// <summary>
		/// List of control reference groups which can be referred to by scenarios.
		/// </summary>
		public ControlCollection<ControlReferenceGroup> Groups
		{
			get
			{
				return _controlReferenceGroups;
			}
		}
		/// <summary>
		/// List of global controls which are posted and/or returned across different scenarios.
		/// </summary>
		public ControlCollection<GlobalControlReference> GlobalControls
		{
			get
			{
				return _globalControls;
			}
		}
		/// <summary>
		/// List of hash string providers - controls which provide a part of the returned hash string.
		/// </summary>
		public List<IHashStringPartProvider> HashStringProviders
		{
			get
			{
				return this._hashStringProviders;
			}
		}
		//public List<Script> OptionalScripts
		//{
		//  get
		//  {
		//    return this._optionalScripts;
		//  }
		//}
		#endregion
		#region AGGREGATES
		/// <summary>
		/// All scenarios thru-out the page.
		/// </summary>
		public List<PostScenario> AllScenarios
		{
			get
			{
				//if (_allScenarios == null)
				//{
				//  _allScenarios = new List<PostScenario>();
				//  foreach (Manager mgr in Managers)
				//  {
				//    _allScenarios.AddRange(mgr.Scenarios);
				//  }
				//  _allScenarios.AddRange(Scenarios);
				//}
				return this._allScenarios;
			}
		}
		/// <summary>
		/// All global controls thru-out the page.
		/// </summary>
		public IEnumerable<GlobalControlReference> AllGlobalControls
		{
			get
			{
				List<GlobalControlReference> res = new List<GlobalControlReference>();
				foreach (Manager mgr in Managers)
				{
					res.AddRange(mgr.GlobalControls);
				}
				res.AddRange(GlobalControls);
				return res;
			}
		}
		/// <summary>
		/// All script and style includes thru-out the page.
		/// </summary>
		public Dictionary<string, BaseInclude> AllIncludes
		{
			get
			{
				return _allIncludes;
			}
		}
		/// <summary>
		/// All control reference groups thru-out the page.
		/// </summary>
		public IEnumerable<ControlReferenceGroup> AllControlReferenceGroups
		{
			get
			{
				List<ControlReferenceGroup> res = new List<ControlReferenceGroup>();
				foreach (Manager mgr in Managers)
				{
					res.AddRange(mgr.Groups);
				}
				res.AddRange(Groups);
				return res;
			}
		}
		#endregion
		#region OPTIMIZATION
		/// <summary>
		/// Gets the JS obfuscator which was used to obfuscate the client scripts for this page.
		/// </summary>
		public JSObfuscator Obfuscator { get { return this._obfuscator; } }
		/// <summary>
		/// Gets true if script includes should be combined into one, otherwise false.
		/// </summary>
		public virtual bool CombineScripts
		{
			get
			{
				return !HttpContext.Current.IsDebuggingEnabled;
			}
		}
		/// <summary>
		/// Gets true if style includes should be combined by containing folder path, otherwise false.
		/// </summary>
		public virtual bool CombineStyles
		{
			get
			{
				return !HttpContext.Current.IsDebuggingEnabled;
			}
		}
		/// <summary>
		/// Gets true if scripts should be optimized (obfuscated and compressed), otherwise false.
		/// </summary>
		/// <remarks>
		/// This flag is used for all script includes and all snippets that are "spit out" in addition to the includes,
		/// for ex., event handler scripts.
		/// </remarks>
		public virtual bool OptimizeScripts
		{
			get
			{
				return !HttpContext.Current.IsDebuggingEnabled;
			}
		}
		/// <summary>
		/// Gets true if CSS files should be optimized (obfuscated and compressed), otherwise false.
		/// </summary>
		public virtual bool OptimizeStyles
		{
			get
			{
				return !HttpContext.Current.IsDebuggingEnabled;
			}
		}
		/// <summary>
		/// Gets true if control IDs should be shortened, otherwise false.
		/// </summary>
		public virtual bool OptimizeIDs
		{
			get
			{
				return !HttpContext.Current.IsDebuggingEnabled;
			}
		}
		#endregion
		#region OTHER
		/// <summary>
		/// Gets a repository for items that are relevant to this instance of TPage, that is, across requests.
		/// </summary>
		public Dictionary<string, object> PageItems
		{
			get
			{
				return this._pageItems;
			}
		}
		/// <summary>
		/// Gets the type of browser for which this TPage is suitable.
		/// </summary>
		public BrowserType BrowserType
		{
			get
			{
				return this._browserType;
			}
		}
		#endregion
		#endregion
		#region METHODS
		/// <summary>
		/// Registers a control as one whose control state must be persisted.
		/// </summary>
		/// <param name="control">The control to register.</param>
		public void RegisterControlState(IHasControlState control)
		{
			if (!this._controlsRequiringControlState.Contains(control))
			{
				this._controlsRequiringControlState.Add(control);
			}
		}
		#endregion
	}
}